package com.org.zp.ctrl.company;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.org.base.ctrl.ActivityTaskManager;
import com.org.base.ctrl.BaseActivity;
import com.org.base.util.HttpUtils;
import com.org.base.util.URLHelper;
import com.org.zp.R;
import com.org.zp.app.RecApp;
import com.org.zp.entity.company.BusinessSchoolIcon;
import com.org.zp.entity.company.CompanyPosition;

public class BusinessSchoolActivity extends BaseActivity implements
		OnClickListener {
	/** Called when the activity is first created. */

	private RecApp app;

	private TextView title;
	private Button back_btn;
	private Button enable_btn;

	private ImageView com_business_school_1, com_business_school_2,
			com_business_school_3, com_business_school_4,
			com_business_school_5, com_business_school_6,
			com_business_school_7, com_business_school_8;
	
	private List<BusinessSchoolIcon> mSchoolInfo = new ArrayList<BusinessSchoolIcon>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.com_business_school);

		app = getMyApplication();

		if (!(ActivityTaskManager.getInstance()
				.containsName("BusinessSchoolActivity"))) {
			ActivityTaskManager.getInstance().putActivity(
					"BusinessSchoolActivity", this);
		}

		back_btn = (Button) findViewById(R.id.back);
		enable_btn = (Button) findViewById(R.id.top_right_button);
		enable_btn.setVisibility(View.INVISIBLE);
		back_btn.setText("  返回");
		back_btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ActivityTaskManager.getInstance().removeActivity(
						"BusinessSchoolActivity");
			}
		});
		title = (TextView) findViewById(R.id.headTitle);
		title.setText("企业商学院");

		init();
	}

	private void init() {

		com_business_school_1 = (ImageView) findViewById(R.id.com_business_school_1);
		com_business_school_1.setOnClickListener(this);
		com_business_school_2 = (ImageView) findViewById(R.id.com_business_school_2);
		com_business_school_2.setOnClickListener(this);
		com_business_school_3 = (ImageView) findViewById(R.id.com_business_school_3);
		com_business_school_3.setOnClickListener(this);
		com_business_school_4 = (ImageView) findViewById(R.id.com_business_school_4);
		com_business_school_4.setOnClickListener(this);
		com_business_school_5 = (ImageView) findViewById(R.id.com_business_school_5);
		com_business_school_5.setOnClickListener(this);
		com_business_school_6 = (ImageView) findViewById(R.id.com_business_school_6);
		com_business_school_6.setOnClickListener(this);
		com_business_school_7 = (ImageView) findViewById(R.id.com_business_school_7);
		com_business_school_7.setOnClickListener(this);
		com_business_school_8 = (ImageView) findViewById(R.id.com_business_school_8);
		com_business_school_8.setOnClickListener(this);
		
		new getSchoolIconComInfo().execute(URLHelper.mPrefix);

	}
	
	public void onClick(View v) {
		Intent intent = new Intent(BusinessSchoolActivity.this,
				BusinessSchoolListActivity.class);
		
		if (v == com_business_school_1) {
			Log.e("TMG", "in click 1");
			if(mSchoolInfo.size() > 0){
				intent.putExtra("id", mSchoolInfo.get(0).getId());
				startActivity(intent);
			}
			
		}else if (v == com_business_school_2) {
			Log.e("TMG", "in click 2");
			if(mSchoolInfo.size() > 0){
				intent.putExtra("id", mSchoolInfo.get(1).getId());
				startActivity(intent);
			}
			
		}else if (v == com_business_school_3) {
			Log.e("TMG", "in click 3");
			if(mSchoolInfo.size() > 0){
				intent.putExtra("id", mSchoolInfo.get(2).getId());
				startActivity(intent);
			}
			
		}else if (v == com_business_school_4) {
			Log.e("TMG", "in click 4");
			if(mSchoolInfo.size() > 0){
				intent.putExtra("id", mSchoolInfo.get(3).getId());
				startActivity(intent);
			}
			
		}else if (v == com_business_school_5) {
			Log.e("TMG", "in click 5");
			if(mSchoolInfo.size() > 0){
				intent.putExtra("id", mSchoolInfo.get(4).getId());
				startActivity(intent);
			}
			
		}else if (v == com_business_school_6) {
			Log.e("TMG", "in click 6");
			if(mSchoolInfo.size() > 0){
				intent.putExtra("id", mSchoolInfo.get(5).getId());
				startActivity(intent);
			}
			
		}else if (v == com_business_school_7) {
			Log.e("TMG", "in click 7");
			if(mSchoolInfo.size() > 0){
				intent.putExtra("id", mSchoolInfo.get(6).getId());
				startActivity(intent);
			}
			
		}else if (v == com_business_school_8) {
			Log.e("TMG", "in click 8");
			if(mSchoolInfo.size() > 0){
				intent.putExtra("id", mSchoolInfo.get(7).getId());
				startActivity(intent);
			}
			
		}
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			ActivityTaskManager.getInstance().removeActivity(
					"BusinessSchoolActivity");
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	class getSchoolIconComInfo extends AsyncTask<String, Void, String> {

		@Override
		protected String doInBackground(String... params) {
			Map<String, String> map = new HashMap<String, String>(); // 登录时向服务器发送的参数
			map.put("act", "businessCollegeList");

			return HttpUtils.readHttp(params[0], map);// 网络操作，向服务器发送登录请求，获取返回信息

		}

		@Override
		protected void onPostExecute(String result) {
			
			Log.e("TMG", "resume info = " + result);

			if (result == null || result.equals("")) {
				// 发送解析数据 消息
				Toast toast = Toast.makeText(BusinessSchoolActivity.this, "失败",
						Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();

			} else if (result.equals("timeout")) {
				// 发送解析数据 消息
				Toast toast = Toast.makeText(BusinessSchoolActivity.this,
						"网络超时", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();

			} else {
				try {
					JSONObject json = new JSONObject(result);
					
					Log.e("TMG", "result = " + result);
					
					String succeed = json.getString("succeed");
					if (succeed.equals("000")) {
						
						String data = json.getString("dataList");
						JSONArray list = new JSONArray(data);

						for (int i = 0; i < list.length(); i++) {
							BusinessSchoolIcon info = new BusinessSchoolIcon();
							JSONObject log = list.getJSONObject(i);
							info.setId(log.getString("id"));
							info.setName(log.getString("name"));
							info.setImg(log.getString("img"));
							mSchoolInfo.add(info);
						}
						
						Log.e("TMG", "school size = " + Integer.toString(mSchoolInfo.size()));
						if(mSchoolInfo.size() == 8){
							if ("".equals(mSchoolInfo.get(0).getImg())) {

							}else{
								
								Log.e("TMG", "img url = " + URLHelper.mPrefixImg + mSchoolInfo.get(0).getImg());
								app.imageLoader.DisplayImage(URLHelper.mPrefixImg + mSchoolInfo.get(0).getImg(),
										com_business_school_1, R.drawable.com_business_school_1);
							}
							
							if ("".equals(mSchoolInfo.get(1).getImg())) {

							}else{
								app.imageLoader.DisplayImage(URLHelper.mPrefixImg + mSchoolInfo.get(1).getImg(),
										com_business_school_2, R.drawable.com_business_school_2);
							}
							
							if ("".equals(mSchoolInfo.get(2).getImg())) {

							}else{
								app.imageLoader.DisplayImage(URLHelper.mPrefixImg + mSchoolInfo.get(2).getImg(),
										com_business_school_3, R.drawable.com_business_school_3);
							}
							
							if ("".equals(mSchoolInfo.get(3).getImg())) {

							}else{
								app.imageLoader.DisplayImage(URLHelper.mPrefixImg + mSchoolInfo.get(3).getImg(),
										com_business_school_4, R.drawable.com_business_school_4);
							}
							
							if ("".equals(mSchoolInfo.get(4).getImg())) {

							}else{
								app.imageLoader.DisplayImage(URLHelper.mPrefixImg + mSchoolInfo.get(4).getImg(),
										com_business_school_5, R.drawable.com_business_school_5);
							}
							
							if ("".equals(mSchoolInfo.get(5).getImg())) {

							}else{
								app.imageLoader.DisplayImage(URLHelper.mPrefixImg + mSchoolInfo.get(5).getImg(),
										com_business_school_6, R.drawable.com_business_school_6);
							}
							
							if ("".equals(mSchoolInfo.get(6).getImg())) {

							}else{
								app.imageLoader.DisplayImage(URLHelper.mPrefixImg + mSchoolInfo.get(6).getImg(),
										com_business_school_7, R.drawable.com_business_school_7);
							}
							
							if ("".equals(mSchoolInfo.get(7).getImg())) {

							}else{
								app.imageLoader.DisplayImage(URLHelper.mPrefixImg + mSchoolInfo.get(7).getImg(),
										com_business_school_8, R.drawable.com_business_school_8);
							}
						}
						
						
						
					} else {
						
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}

		}
	}

}